package vip.ifmm.lehouse.api.gateway.controller.user;

import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ifmm.lehouse.api.gateway.annotation.FromSession;
import vip.ifmm.lehouse.api.gateway.utils.CookieUtil;
import vip.ifmm.lehouse.common.bo.SessionBo;
import vip.ifmm.lehouse.common.dto.ServerResponse;
import vip.ifmm.lehouse.common.enums.status.UserLoginStatus;
import vip.ifmm.lehouse.common.model.User;
import vip.ifmm.lehouse.user.service.api.UserLoginService;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录网关
 *
 * @author Red
 * email: 1318944013@qq.com
 * date: 2019/6/16 20:21
 */
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserLoginController {

    // 远程引用UserLoginService
    @Reference(owner = "red")
    private UserLoginService userLoginService = null;

    // 处理登录请求
    @PostMapping("/login")
    public ServerResponse login(User user, HttpServletResponse response) {
        // 执行登录逻辑
        ServerResponse<String> loginResult = userLoginService.login(user);
        // 登录成功, 将token加入Cookie
        if(loginResult.isSuccess()) {
            CookieUtil.addCookie(response, loginResult.getData());
        }
        return loginResult;
    }

    @GetMapping("/session")
    public ServerResponse cookie(@FromSession SessionBo sessionBo) {
        return ServerResponse.response(UserLoginStatus.LOGIN_SUCCESS, sessionBo);
    }

}
